//
//  DYIMChatNavView.m
//  HBuilder
//
//  Created by 黄德玉 on 2019/11/2.
//  Copyright © 2019 DCloud. All rights reserved.
//

#import "DYIMChatNavView.h"

@implementation DYIMChatNavView

- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:self.titleLabel];
        [self addSubview:self.backBtn];
        [self addSubview:self.backImgView];
        [self addSubview:self.btmLine];
        [self _dy_layout];
    }
    return self;
}

- (void)_dy_layout {
    NSLayoutConstraint * centerLC = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.titleLabel attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
    [self addConstraint:centerLC];
    [self.titleLabel addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:200]];
    [self.titleLabel addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:44]];
    NSLayoutConstraint * titleBottomLC = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.titleLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    [self addConstraint:titleBottomLC];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.backBtn attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.backBtn attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    [self.backBtn addConstraint:[NSLayoutConstraint constraintWithItem:self.backBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:90]];
    [self.backBtn addConstraint:[NSLayoutConstraint constraintWithItem:self.backBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:44]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.backImgView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-10]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.backImgView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    [self.backImgView addConstraint:[NSLayoutConstraint constraintWithItem:self.backImgView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:44]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.btmLine attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.btmLine attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.btmLine attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
    [self.btmLine addConstraint:[NSLayoutConstraint constraintWithItem:self.btmLine attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0.5]];
}

- (void)hideBackBtn {
    self.backImgView.hidden = YES;
    self.backBtn.hidden = YES;
}

- (void)btnClicked:(UIButton *)btn{
    if (self.itemClicked) {
        self.itemClicked(btn.tag);
    }
}


#pragma mark - 初始化
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _titleLabel.textColor = HexRGB(0x343b43);
        _titleLabel.font = [UIFont boldSystemFontOfSize:16];
    }
    return _titleLabel;
}

- (UIButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setTitle:@"" forState:UIControlStateNormal];
        [_backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _backBtn.translatesAutoresizingMaskIntoConstraints = NO;
        _backBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_backBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        _backBtn.tag = 0;
    }
    return _backBtn;
}

- (UIImageView *)backImgView {
    if (!_backImgView) {
        _backImgView = [[UIImageView alloc] init];
        _backImgView.image = [UIImage imageNamed:@"icon_backarrow"];
        _backImgView.translatesAutoresizingMaskIntoConstraints = NO;
        _backImgView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _backImgView;
}

- (UIView *)btmLine {
    if (!_btmLine) {
        _btmLine = [UIView new];
        _btmLine.translatesAutoresizingMaskIntoConstraints = NO;
        _btmLine.backgroundColor = HexRGB(0xd8d8d8);
    }
    return _btmLine;
}

@end
